package fms.core;

import org.apache.log4j.Logger;

import fms.entity.CptSend;

/**
 * FPN/RI:DA:ZJHK:AA:ZBAA:F:LH.R339..BH.R339..BHY.H82..LBN.R343..LKO.A461..HG+ZB
 * (FPN/RI:DA:起飞机场:AA:目的机场:F:航路走向)
 * */

public class FLPMsgBuilder {
	private static Logger logger = Logger.getLogger(FLPMsgBuilder.class);

	private static final String flpRespTextHead = "FPN/RI";

	public static String build(CptSend cptSend) {
		logger.info("飞行计划组报开始");
		StringBuffer responseFreeText = new StringBuffer();
		try {
			responseFreeText.append(flpRespTextHead);
			responseFreeText.append(":DA");
			responseFreeText.append(":");
			responseFreeText.append(cptSend.getDep());
			responseFreeText.append(":AA");
			responseFreeText.append(":");
			responseFreeText.append(cptSend.getArr());
			responseFreeText.append(":F");
			responseFreeText.append(":");
			
			String routeLine = cptSend.getRouteline();
			// ZJHK-LH/R339/BHY/H82/LBN/R343/LKO/A461/HG+ZB-ZBAA
			// LH.R339..BH.R339..BHY.H82..LBN.R343..LKO.A461..HG+ZB
			String routeArr[] = routeLine.split("\\/");
			for (int i = 0; i < routeArr.length; i++) {
				if (i == 0) {
					String route1[] = routeArr[i].split("-");
					responseFreeText.append(route1[1]);
					if (i % 2 == 0) {
						responseFreeText.append(".");
					} else {
						responseFreeText.append("..");
					}
				} else if (i == routeArr.length - 1) {
					String routeend[] = routeArr[i].split("\\-");
					 if(routeend.length<=1) {
	                        responseFreeText.append(routeend[0]);
	                  }
				} else {				
					String rarr[] = routeArr[i].split("\\-");
					if(rarr.length<=1){	
						responseFreeText.append(routeArr[i]);
						if (i % 2 == 0) {
							responseFreeText.append(".");
						} else {
							responseFreeText.append("..");
						}				
					}else{						
						if (i % 2 == 0) {
							responseFreeText.append(".");
						} else {
							responseFreeText.append("..");
						}	
					}
				}

			}
			String returnStr = responseFreeText.toString().replaceAll("\n", "");
			returnStr = returnStr.replaceAll("\t", "");
			returnStr=returnStr.replaceAll(" ", "");	 
			logger.info("飞行计划组报结束");
			return returnStr;
		} catch (Exception e) {
			// e.printStackTrace();
			logger.error(e.getMessage() + ":飞行计划组报异常！");
			return "";
		}

	}
	

 
	
	

 

}